00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/dict.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include <stdlib.h>
00027
00028 #define RPL_SIGNATURE "ARMovie\x0A"
00029 #define RPL_SIGNATURE_SIZE 8
00030
00032 #define RPL_LINE_LENGTH 256
00033
00034 static int rpl_probe(AVProbeData *p)
00035 {
00036 if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
00037 return 0;
00038
00039 return AVPROBE_SCORE_MAX;
00040 }
00041
00042 typedef struct RPLContext {
00043
00044 int32_t frames_per_chunk;
00045
00046
00047 uint32_t chunk_number;
00048 uint32_t chunk_part;
00049 uint32_t frame_in_part;
00050 } RPLContext;
00051
00052 static int read_line(AVIOContext * pb, char* line, int bufsize)
00053 {
00054 int i;
00055 for (i = 0; i < bufsize - 1; i++) {
00056 int b = avio_r8(pb);
00057 if (b == 0)
00058 break;
00059 if (b == '\n') {
00060 line[i] = '\0';
00061 return 0;
00062 }
00063 line[i] = b;
00064 }
00065 line[i] = '\0';
00066 return -1;
00067 }
00068
00069 static int32_t read_int(const char* line, const char** endptr, int* error)
00070 {
00071 unsigned long result = 0;
00072 for (; *line>='0' && *line<='9'; line++) {
00073 if (result > (0x7FFFFFFF - 9) / 10)
00074 *error = -1;
00075 result = 10 * result + *line - '0';
00076 }
00077 *endptr = line;
00078 return result;
00079 }
00080
00081 static int32_t read_line_and_int(AVIOContext * pb, int* error)
00082 {
00083 char line[RPL_LINE_LENGTH];
00084 const char *endptr;
00085 *error |= read_line(pb, line, sizeof(line));
00086 return read_int(line, &endptr, error);
00087 }
00088
00093 static AVRational read_fps(const char* line, int* error)
00094 {
00095 int64_t num, den = 1;
00096 AVRational result;
00097 num = read_int(line, &line, error);
00098 if (*line == '.')
00099 line++;
00100 for (; *line>='0' && *line<='9'; line++) {
00101
00102 if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
00103 break;
00104 num = 10 * num + *line - '0';
00105 den *= 10;
00106 }
00107 if (!num)
00108 *error = -1;
00109 av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
00110 return result;
00111 }
00112
00113 static int rpl_read_header(AVFormatContext *s, AVFormatParameters *ap)
00114 {
00115 AVIOContext *pb = s->pb;
00116 RPLContext *rpl = s->priv_data;
00117 AVStream *vst = NULL, *ast = NULL;
00118 int total_audio_size;
00119 int error = 0;
00120
00121 uint32_t i;
00122
00123 int32_t audio_format, chunk_catalog_offset, number_of_chunks;
00124 AVRational fps;
00125
00126 char line[RPL_LINE_LENGTH];
00127
00128
00129
00130
00131
00132
00133
00134 error |= read_line(pb, line, sizeof(line));
00135 error |= read_line(pb, line, sizeof(line));
00136 av_dict_set(&s->metadata, "title" , line, 0);
00137 error |= read_line(pb, line, sizeof(line));
00138 av_dict_set(&s->metadata, "copyright", line, 0);
00139 error |= read_line(pb, line, sizeof(line));
00140 av_dict_set(&s->metadata, "author" , line, 0);
00141
00142
00143 vst = avformat_new_stream(s, NULL);
00144 if (!vst)
00145 return AVERROR(ENOMEM);
00146 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00147 vst->codec->codec_tag = read_line_and_int(pb, &error);
00148 vst->codec->width = read_line_and_int(pb, &error);
00149 vst->codec->height = read_line_and_int(pb, &error);
00150 vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error);
00151 error |= read_line(pb, line, sizeof(line));
00152 fps = read_fps(line, &error);
00153 avpriv_set_pts_info(vst, 32, fps.den, fps.num);
00154
00155
00156 switch (vst->codec->codec_tag) {
00157 #if 0
00158 case 122:
00159 vst->codec->codec_id = CODEC_ID_ESCAPE122;
00160 break;
00161 #endif
00162 case 124:
00163 vst->codec->codec_id = CODEC_ID_ESCAPE124;
00164
00165 vst->codec->bits_per_coded_sample = 16;
00166 break;
00167 #if 0
00168 case 130:
00169 vst->codec->codec_id = CODEC_ID_ESCAPE130;
00170 break;
00171 #endif
00172 default:
00173 av_log(s, AV_LOG_WARNING,
00174 "RPL video format %i not supported yet!\n",
00175 vst->codec->codec_tag);
00176 vst->codec->codec_id = CODEC_ID_NONE;
00177 }
00178
00179
00180
00181
00182
00183 audio_format = read_line_and_int(pb, &error);
00184 if (audio_format) {
00185 ast = avformat_new_stream(s, NULL);
00186 if (!ast)
00187 return AVERROR(ENOMEM);
00188 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00189 ast->codec->codec_tag = audio_format;
00190 ast->codec->sample_rate = read_line_and_int(pb, &error);
00191 ast->codec->channels = read_line_and_int(pb, &error);
00192 ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error);
00193
00194
00195 if (ast->codec->bits_per_coded_sample == 0)
00196 ast->codec->bits_per_coded_sample = 4;
00197
00198 ast->codec->bit_rate = ast->codec->sample_rate *
00199 ast->codec->bits_per_coded_sample *
00200 ast->codec->channels;
00201
00202 ast->codec->codec_id = CODEC_ID_NONE;
00203 switch (audio_format) {
00204 case 1:
00205 if (ast->codec->bits_per_coded_sample == 16) {
00206
00207 ast->codec->codec_id = CODEC_ID_PCM_S16LE;
00208 break;
00209 }
00210
00211
00212 break;
00213 case 101:
00214 if (ast->codec->bits_per_coded_sample == 8) {
00215
00216
00217 ast->codec->codec_id = CODEC_ID_PCM_U8;
00218 break;
00219 } else if (ast->codec->bits_per_coded_sample == 4) {
00220 ast->codec->codec_id = CODEC_ID_ADPCM_IMA_EA_SEAD;
00221 break;
00222 }
00223 break;
00224 }
00225 if (ast->codec->codec_id == CODEC_ID_NONE) {
00226 av_log(s, AV_LOG_WARNING,
00227 "RPL audio format %i not supported yet!\n",
00228 audio_format);
00229 }
00230 avpriv_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
00231 } else {
00232 for (i = 0; i < 3; i++)
00233 error |= read_line(pb, line, sizeof(line));
00234 }
00235
00236 rpl->frames_per_chunk = read_line_and_int(pb, &error);
00237 if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
00238 av_log(s, AV_LOG_WARNING,
00239 "Don't know how to split frames for video format %i. "
00240 "Video stream will be broken!\n", vst->codec->codec_tag);
00241
00242 number_of_chunks = read_line_and_int(pb, &error);
00243
00244 number_of_chunks++;
00245
00246 error |= read_line(pb, line, sizeof(line));
00247 error |= read_line(pb, line, sizeof(line));
00248 chunk_catalog_offset =
00249 read_line_and_int(pb, &error);
00250 error |= read_line(pb, line, sizeof(line));
00251 error |= read_line(pb, line, sizeof(line));
00252 error |= read_line(pb, line, sizeof(line));
00253
00254
00255 avio_seek(pb, chunk_catalog_offset, SEEK_SET);
00256 total_audio_size = 0;
00257 for (i = 0; i < number_of_chunks; i++) {
00258 int64_t offset, video_size, audio_size;
00259 error |= read_line(pb, line, sizeof(line));
00260 if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
00261 &offset, &video_size, &audio_size))
00262 error = -1;
00263 av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
00264 video_size, rpl->frames_per_chunk, 0);
00265 if (ast)
00266 av_add_index_entry(ast, offset + video_size, total_audio_size,
00267 audio_size, audio_size * 8, 0);
00268 total_audio_size += audio_size * 8;
00269 }
00270
00271 if (error) return AVERROR(EIO);
00272
00273 return 0;
00274 }
00275
00276 static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
00277 {
00278 RPLContext *rpl = s->priv_data;
00279 AVIOContext *pb = s->pb;
00280 AVStream* stream;
00281 AVIndexEntry* index_entry;
00282 uint32_t ret;
00283
00284 if (rpl->chunk_part == s->nb_streams) {
00285 rpl->chunk_number++;
00286 rpl->chunk_part = 0;
00287 }
00288
00289 stream = s->streams[rpl->chunk_part];
00290
00291 if (rpl->chunk_number >= stream->nb_index_entries)
00292 return -1;
00293
00294 index_entry = &stream->index_entries[rpl->chunk_number];
00295
00296 if (rpl->frame_in_part == 0)
00297 if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
00298 return AVERROR(EIO);
00299
00300 if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00301 stream->codec->codec_tag == 124) {
00302
00303
00304 uint32_t frame_size;
00305
00306 avio_skip(pb, 4);
00307 frame_size = avio_rl32(pb);
00308 if (avio_seek(pb, -8, SEEK_CUR) < 0)
00309 return AVERROR(EIO);
00310
00311 ret = av_get_packet(pb, pkt, frame_size);
00312 if (ret != frame_size) {
00313 av_free_packet(pkt);
00314 return AVERROR(EIO);
00315 }
00316 pkt->duration = 1;
00317 pkt->pts = index_entry->timestamp + rpl->frame_in_part;
00318 pkt->stream_index = rpl->chunk_part;
00319
00320 rpl->frame_in_part++;
00321 if (rpl->frame_in_part == rpl->frames_per_chunk) {
00322 rpl->frame_in_part = 0;
00323 rpl->chunk_part++;
00324 }
00325 } else {
00326 ret = av_get_packet(pb, pkt, index_entry->size);
00327 if (ret != index_entry->size) {
00328 av_free_packet(pkt);
00329 return AVERROR(EIO);
00330 }
00331
00332 if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00333
00334
00335 pkt->duration = rpl->frames_per_chunk;
00336 } else {
00337
00338
00339 pkt->duration = ret * 8;
00340 }
00341 pkt->pts = index_entry->timestamp;
00342 pkt->stream_index = rpl->chunk_part;
00343 rpl->chunk_part++;
00344 }
00345
00346
00347
00348 if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
00349 pkt->flags |= AV_PKT_FLAG_KEY;
00350
00351 return ret;
00352 }
00353
00354 AVInputFormat ff_rpl_demuxer = {
00355 .name = "rpl",
00356 .long_name = NULL_IF_CONFIG_SMALL("RPL/ARMovie format"),
00357 .priv_data_size = sizeof(RPLContext),
00358 .read_probe = rpl_probe,
00359 .read_header = rpl_read_header,
00360 .read_packet = rpl_read_packet,
00361 };