lavc/j2kenc: Support AV_PIX_FMT_PAL8.
[ffmpeg.git] / libavcodec / tak_parser.c
index dcc37ec..835a47b 100644 (file)
  * TAK parser
  **/
 
+#define BITSTREAM_READER_LE
 #include "parser.h"
 #include "tak.h"
 
 typedef struct TAKParseContext {
     ParseContext  pc;
     TAKStreamInfo ti;
-    int index;
+    int           index;
 } TAKParseContext;
 
-static av_cold int tak_init(AVCodecParserContext *s)
-{
-    ff_tak_init_crc();
-    return 0;
-}
-
 static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
                      const uint8_t **poutbuf, int *poutbuf_size,
                      const uint8_t *buf, int buf_size)
 {
     TAKParseContext *t = s->priv_data;
-    ParseContext *pc = &t->pc;
-    int next = END_NOT_FOUND;
+    ParseContext *pc   = &t->pc;
+    int next           = END_NOT_FOUND;
     GetBitContext gb;
     int consumed = 0;
-    int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
+    int needed   = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
+    int ret;
 
     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
         TAKStreamInfo ti;
-        init_get_bits(&gb, buf, buf_size);
+        if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
+            return ret;
         if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
-            s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples :
-                                                     t->ti.frame_samples;
+            s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
+                                                   : t->ti.frame_samples;
         *poutbuf      = buf;
         *poutbuf_size = buf_size;
         return buf_size;
@@ -63,10 +60,12 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
 
     while (buf_size || t->index + needed <= pc->index) {
         if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
-            int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES, buf_size);
+            int tmp_buf_size       = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
+                                           buf_size);
             const uint8_t *tmp_buf = buf;
 
-            ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);
+            if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
+                return AVERROR(ENOMEM);
             consumed += tmp_buf_size;
             buf      += tmp_buf_size;
             buf_size -= tmp_buf_size;
@@ -77,21 +76,23 @@ static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
                 pc->buffer[ t->index + 1 ] == 0xA0) {
                 TAKStreamInfo ti;
 
-                init_get_bits(&gb, pc->buffer + t->index,
-                              8 * (pc->index - t->index));
+                if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
+                                          pc->index - t->index)) < 0)
+                    return ret;
                 if (!ff_tak_decode_frame_header(avctx, &gb,
                         pc->frame_start_found ? &ti : &t->ti, 127) &&
                     !ff_tak_check_crc(pc->buffer + t->index,
                                       get_bits_count(&gb) / 8)) {
                     if (!pc->frame_start_found) {
                         pc->frame_start_found = 1;
-                        s->duration = t->ti.last_frame_samples ?
-                                      t->ti.last_frame_samples :
-                                      t->ti.frame_samples;
+                        s->duration           = t->ti.last_frame_samples ?
+                                                t->ti.last_frame_samples :
+                                                t->ti.frame_samples;
+                        s->key_frame          = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
                     } else {
                         pc->frame_start_found = 0;
-                        next = t->index - pc->index;
-                        t->index = 0;
+                        next                  = t->index - pc->index;
+                        t->index              = 0;
                         goto found;
                     }
                 }
@@ -108,7 +109,7 @@ found:
     }
 
     if (next != END_NOT_FOUND) {
-        next += consumed;
+        next        += consumed;
         pc->overread = FFMAX(0, -next);
     }
 
@@ -120,7 +121,6 @@ found:
 AVCodecParser ff_tak_parser = {
     .codec_ids      = { AV_CODEC_ID_TAK },
     .priv_data_size = sizeof(TAKParseContext),
-    .parser_init    = tak_init,
     .parser_parse   = tak_parse,
     .parser_close   = ff_parse_close,
 };