Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ffmpeg png streaming support

By: a guest on May 3rd, 2012  |  syntax: C  |  size: 5.58 KB  |  views: 298  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. From 13e99a034a9ccdebdfeb51655009135f3d0a9cd9 Mon Sep 17 00:00:00 2001
  2. From: Eugene Ware <eugene@noblesamurai.com>
  3. Date: Fri, 4 May 2012 00:16:03 +1000
  4. Subject: [PATCH] Support for png image2pipe streaming
  5.  
  6. ---
  7.  libavcodec/Makefile     |    1 +
  8.  libavcodec/allcodecs.c  |    1 +
  9.  libavcodec/png_parser.c |  122 +++++++++++++++++++++++++++++++++++++++++++++++
  10.  3 files changed, 124 insertions(+), 0 deletions(-)
  11.  create mode 100644 libavcodec/png_parser.c
  12.  
  13. diff --git a/libavcodec/Makefile b/libavcodec/Makefile
  14. index 28d4a1c..6fc29f8 100644
  15. --- a/libavcodec/Makefile
  16. +++ b/libavcodec/Makefile
  17. @@ -703,6 +703,7 @@ OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o h263.o \
  18.                                            mpegvideo.o error_resilience.o \
  19.                                            mpeg4videodec.o mpeg4video.o \
  20.                                            ituh263dec.o h263dec.o
  21. +OBJS-$(CONFIG_PNG_PARSER)              += png_parser.o
  22.  OBJS-$(CONFIG_MPEGAUDIO_PARSER)        += mpegaudio_parser.o \
  23.                                            mpegaudiodecheader.o mpegaudiodata.o
  24.  OBJS-$(CONFIG_MPEGVIDEO_PARSER)        += mpegvideo_parser.o    \
  25. diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
  26. index d31d29a..4ebb90c 100644
  27. --- a/libavcodec/allcodecs.c
  28. +++ b/libavcodec/allcodecs.c
  29. @@ -453,6 +453,7 @@ void avcodec_register_all(void)
  30.      REGISTER_PARSER  (MPEG4VIDEO, mpeg4video);
  31.      REGISTER_PARSER  (MPEGAUDIO, mpegaudio);
  32.      REGISTER_PARSER  (MPEGVIDEO, mpegvideo);
  33. +    REGISTER_PARSER  (PNG, png);
  34.      REGISTER_PARSER  (PNM, pnm);
  35.      REGISTER_PARSER  (RV30, rv30);
  36.      REGISTER_PARSER  (RV40, rv40);
  37. diff --git a/libavcodec/png_parser.c b/libavcodec/png_parser.c
  38. new file mode 100644
  39. index 0000000..ddd1be8
  40. --- /dev/null
  41. +++ b/libavcodec/png_parser.c
  42. @@ -0,0 +1,122 @@
  43. +/*
  44. + * PNG parser
  45. + * Copyright (c) 2009 Peter Holik
  46. + *
  47. + * This file is part of FFmpeg.
  48. + *
  49. + * FFmpeg is free software; you can redistribute it and/or
  50. + * modify it under the terms of the GNU Lesser General Public
  51. + * License as published by the Free Software Foundation; either
  52. + * version 2.1 of the License, or (at your option) any later version.
  53. + *
  54. + * FFmpeg is distributed in the hope that it will be useful,
  55. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  57. + * Lesser General Public License for more details.
  58. + *
  59. + * You should have received a copy of the GNU Lesser General Public
  60. + * License along with FFmpeg; if not, write to the Free Software
  61. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  62. + */
  63. +
  64. +/**
  65. + * @file libavcodec/png_parser.c
  66. + * PNG parser
  67. + */
  68. +
  69. +#include "libavutil/intreadwrite.h"
  70. +#include "parser.h"
  71. +
  72. +#define PNGSIG 0x89504e470d0a1a0a
  73. +#define MNGSIG 0x8a4d4e470d0a1a0a
  74. +
  75. +typedef struct PNGParseContext
  76. +{
  77. +    ParseContext pc;
  78. +    uint32_t index;
  79. +    uint32_t chunk_length;
  80. +    uint32_t remaining_size;
  81. +} PNGParseContext;
  82. +
  83. +static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  84. +                     const uint8_t **poutbuf, int *poutbuf_size,
  85. +                     const uint8_t *buf, int buf_size)
  86. +{
  87. +    PNGParseContext *ppc = s->priv_data;
  88. +    int next = END_NOT_FOUND;
  89. +    int i = 0;
  90. +
  91. +    *poutbuf_size = 0;
  92. +    if (buf_size == 0)
  93. +        return 0;
  94. +
  95. +    if (!ppc->pc.frame_start_found) {
  96. +        uint64_t state64 = ppc->pc.state64;
  97. +        for (; i < buf_size; i++) {
  98. +            state64 = (state64 << 8) | buf[i];
  99. +            if (state64 == PNGSIG || state64 == MNGSIG) {
  100. +                i++;
  101. +                ppc->pc.frame_start_found = 1;
  102. +                break;
  103. +            }
  104. +        }
  105. +        ppc->pc.state64 = state64;
  106. +    } else
  107. +        if (ppc->remaining_size) {
  108. +            i = FFMIN(ppc->remaining_size, buf_size);
  109. +            ppc->remaining_size -= i;
  110. +            if (ppc->remaining_size)
  111. +                goto flush;
  112. +            if (ppc->index == -1) {
  113. +                next = i;
  114. +                goto flush;
  115. +            }
  116. +        }
  117. +
  118. +    for (;ppc->pc.frame_start_found && i < buf_size; i++) {
  119. +        ppc->pc.state = (ppc->pc.state<<8) | buf[i];
  120. +        if (ppc->index == 3) {
  121. +            ppc->chunk_length = AV_RL32(&ppc->pc.state);
  122. +            if (ppc->chunk_length > 0x7fffffff) {
  123. +                ppc->index = ppc->pc.frame_start_found = 0;
  124. +                goto flush;
  125. +            }
  126. +            ppc->chunk_length += 4;
  127. +        } else if (ppc->index == 7) {
  128. +            if (ppc->chunk_length >= buf_size - i)
  129. +                    ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
  130. +            if (AV_RB32(&ppc->pc.state) == MKTAG('I', 'E', 'N', 'D')) {
  131. +                if (ppc->remaining_size)
  132. +                    ppc->index = -1;
  133. +                else
  134. +                    next = ppc->chunk_length + i + 1;
  135. +                break;
  136. +            } else {
  137. +                ppc->index = 0;
  138. +                if (ppc->remaining_size)
  139. +                    break;
  140. +                else
  141. +                    i += ppc->chunk_length;
  142. +                continue;
  143. +            }
  144. +        }
  145. +        ppc->index++;
  146. +    }
  147. +flush:
  148. +    if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
  149. +        return buf_size;
  150. +
  151. +    ppc->index = ppc->pc.frame_start_found = 0;
  152. +
  153. +    *poutbuf      = buf;
  154. +    *poutbuf_size = buf_size;
  155. +    return next;
  156. +}
  157. +
  158. +AVCodecParser ff_png_parser = {
  159. +    { CODEC_ID_PNG },
  160. +    sizeof(PNGParseContext),
  161. +    NULL,
  162. +    png_parse,
  163. +    ff_parse_close,
  164. +};
  165. --
  166. 1.7.6