| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MPEG-4 video frame extraction | ||
| 3 | * Copyright (c) 2003 Fabrice Bellard | ||
| 4 | * Copyright (c) 2003 Michael Niedermayer | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #define UNCHECKED_BITSTREAM_READER 1 | ||
| 24 | |||
| 25 | #include "decode.h" | ||
| 26 | #include "parser.h" | ||
| 27 | #include "mpegvideo.h" | ||
| 28 | #include "mpeg4videodec.h" | ||
| 29 | #include "mpeg4videodefs.h" | ||
| 30 | #include "parser_internal.h" | ||
| 31 | |||
| 32 | struct Mp4vParseContext { | ||
| 33 | ParseContext pc; | ||
| 34 | Mpeg4DecContext dec_ctx; | ||
| 35 | int first_picture; | ||
| 36 | }; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Find the end of the current frame in the bitstream. | ||
| 40 | * @return the position of the first byte of the next frame, or -1 | ||
| 41 | */ | ||
| 42 | 8488 | static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) | |
| 43 | { | ||
| 44 | int vop_found, i; | ||
| 45 | uint32_t state; | ||
| 46 | |||
| 47 | 8488 | vop_found = pc->frame_start_found; | |
| 48 | 8488 | state = pc->state; | |
| 49 | |||
| 50 | 8488 | i = 0; | |
| 51 |
2/2✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 7400 times.
|
8488 | if (!vop_found) { |
| 52 |
2/2✓ Branch 0 taken 8481 times.
✓ Branch 1 taken 2 times.
|
8483 | for (i = 0; i < buf_size; i++) { |
| 53 | 8481 | state = (state << 8) | buf[i]; | |
| 54 |
2/2✓ Branch 0 taken 1086 times.
✓ Branch 1 taken 7395 times.
|
8481 | if (state == VOP_STARTCODE) { |
| 55 | 1086 | i++; | |
| 56 | 1086 | vop_found = 1; | |
| 57 | 1086 | break; | |
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 8486 times.
✓ Branch 1 taken 2 times.
|
8488 | if (vop_found) { |
| 63 | /* EOF considered as end of frame */ | ||
| 64 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8460 times.
|
8486 | if (buf_size == 0) |
| 65 | 26 | return 0; | |
| 66 |
2/2✓ Branch 0 taken 8762134 times.
✓ Branch 1 taken 7387 times.
|
8769521 | for (; i < buf_size; i++) { |
| 67 | 8762134 | state = (state << 8) | buf[i]; | |
| 68 |
2/2✓ Branch 0 taken 1103 times.
✓ Branch 1 taken 8761031 times.
|
8762134 | if ((state & 0xFFFFFF00) == 0x100) { |
| 69 |
3/4✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1073 times.
|
1103 | if (state == SLICE_STARTCODE || state == EXT_STARTCODE) |
| 70 | 30 | continue; | |
| 71 | 1073 | pc->frame_start_found = 0; | |
| 72 | 1073 | pc->state = -1; | |
| 73 | 1073 | return i - 3; | |
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | 7389 | pc->frame_start_found = vop_found; | |
| 78 | 7389 | pc->state = state; | |
| 79 | 7389 | return END_NOT_FOUND; | |
| 80 | } | ||
| 81 | |||
| 82 | /* XXX: make it use less memory */ | ||
| 83 | 3601 | static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx, | |
| 84 | const uint8_t *buf, int buf_size) | ||
| 85 | { | ||
| 86 | 3601 | struct Mp4vParseContext *pc = s1->priv_data; | |
| 87 | 3601 | Mpeg4DecContext *dec_ctx = &pc->dec_ctx; | |
| 88 | 3601 | MPVContext *const s = &dec_ctx->h.c; | |
| 89 | 3601 | GetBitContext gb1, *gb = &gb1; | |
| 90 | int ret; | ||
| 91 | |||
| 92 | 3601 | s->avctx = avctx; | |
| 93 | |||
| 94 |
4/4✓ Branch 0 taken 3483 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 241 times.
✓ Branch 3 taken 3242 times.
|
3601 | if (avctx->extradata_size && pc->first_picture) { |
| 95 | 241 | init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8); | |
| 96 | 241 | ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 1, 1); | |
| 97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 241 times.
|
241 | if (ret < 0) |
| 98 | ✗ | av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n"); | |
| 99 | } | ||
| 100 | |||
| 101 | 3601 | init_get_bits(gb, buf, 8 * buf_size); | |
| 102 | 3601 | ret = ff_mpeg4_parse_picture_header(dec_ctx, gb, 0, 1); | |
| 103 | 3601 | avctx->has_b_frames = !s->low_delay; | |
| 104 |
5/6✓ Branch 0 taken 3549 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 3536 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 3536 times.
✗ Branch 5 not taken.
|
3601 | if (s->width && (!avctx->width || !avctx->height || |
| 105 |
3/4✓ Branch 0 taken 3467 times.
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3467 times.
|
3536 | !avctx->coded_width || !avctx->coded_height)) { |
| 106 | 82 | ret = ff_set_dimensions(avctx, s->width, s->height); | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
|
82 | if (ret < 0) |
| 108 | ✗ | return ret; | |
| 109 | } | ||
| 110 |
5/6✓ Branch 0 taken 885 times.
✓ Branch 1 taken 2716 times.
✓ Branch 2 taken 885 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 875 times.
✓ Branch 5 taken 10 times.
|
3601 | if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->framerate.num>0 && ret>=0){ |
| 111 | av_assert1(s1->pts == AV_NOPTS_VALUE); | ||
| 112 | av_assert1(s1->dts == AV_NOPTS_VALUE); | ||
| 113 | |||
| 114 | 875 | s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->framerate.num}, (AVRational){1, 1200000}); | |
| 115 | } | ||
| 116 | |||
| 117 | 3601 | s1->pict_type = s->pict_type; | |
| 118 | 3601 | pc->first_picture = 0; | |
| 119 | 3601 | return ret; | |
| 120 | } | ||
| 121 | |||
| 122 | 337 | static av_cold int mpeg4video_parse_init(AVCodecParserContext *s) | |
| 123 | { | ||
| 124 | 337 | struct Mp4vParseContext *pc = s->priv_data; | |
| 125 | |||
| 126 | 337 | pc->first_picture = 1; | |
| 127 | 337 | pc->dec_ctx.quant_precision = 5; | |
| 128 | 337 | pc->dec_ctx.h.c.slice_context_count = 1; | |
| 129 | 337 | pc->dec_ctx.showed_packed_warning = 1; | |
| 130 | 337 | return 0; | |
| 131 | } | ||
| 132 | |||
| 133 | 10990 | static int mpeg4video_parse(AVCodecParserContext *s, | |
| 134 | AVCodecContext *avctx, | ||
| 135 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 136 | const uint8_t *buf, int buf_size) | ||
| 137 | { | ||
| 138 | 10990 | ParseContext *pc = s->priv_data; | |
| 139 | int next; | ||
| 140 | |||
| 141 |
2/2✓ Branch 0 taken 2502 times.
✓ Branch 1 taken 8488 times.
|
10990 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 142 | 2502 | next = buf_size; | |
| 143 | } else { | ||
| 144 | 8488 | next = mpeg4_find_frame_end(pc, buf, buf_size); | |
| 145 | |||
| 146 |
2/2✓ Branch 1 taken 7389 times.
✓ Branch 2 taken 1099 times.
|
8488 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
| 147 | 7389 | *poutbuf = NULL; | |
| 148 | 7389 | *poutbuf_size = 0; | |
| 149 | 7389 | return buf_size; | |
| 150 | } | ||
| 151 | } | ||
| 152 | 3601 | mpeg4_decode_header(s, avctx, buf, buf_size); | |
| 153 | |||
| 154 | 3601 | *poutbuf = buf; | |
| 155 | 3601 | *poutbuf_size = buf_size; | |
| 156 | 3601 | return next; | |
| 157 | } | ||
| 158 | |||
| 159 | const FFCodecParser ff_mpeg4video_parser = { | ||
| 160 | PARSER_CODEC_LIST(AV_CODEC_ID_MPEG4), | ||
| 161 | .priv_data_size = sizeof(struct Mp4vParseContext), | ||
| 162 | .init = mpeg4video_parse_init, | ||
| 163 | .parse = mpeg4video_parse, | ||
| 164 | .close = ff_parse_close, | ||
| 165 | }; | ||
| 166 |