mpeg_extractor.c (5581B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2004, 2005, 2006, 2009, 2012 Vidyut Samanta and Christian Grothoff 4 5 libextractor is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 libextractor is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with libextractor; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 21 /** 22 * @file plugins/mpeg_extractor.c 23 * @brief plugin to support MPEG files 24 * @author Christian Grothoff 25 */ 26 #include "platform.h" 27 #include "extractor.h" 28 #include <mpeg2dec/mpeg2.h> 29 30 31 /** 32 * Give meta data to extractor. 33 * 34 * @param t type of the meta data 35 * @param s meta data value in utf8 format 36 */ 37 #define ADD(s,t) do { if (0 != ec->proc (ec->cls, "mpeg", t, \ 38 EXTRACTOR_METAFORMAT_UTF8, \ 39 "text/plain", s, strlen (s) \ 40 + 1)) goto EXIT; \ 41 } while (0) 42 43 44 /** 45 * Main entry method for the 'video/mpeg' extraction plugin. 46 * 47 * @param ec extraction context provided to the plugin 48 */ 49 void 50 EXTRACTOR_mpeg_extract_method (struct EXTRACTOR_ExtractContext *ec); 51 52 void 53 EXTRACTOR_mpeg_extract_method (struct EXTRACTOR_ExtractContext *ec) 54 { 55 mpeg2dec_t *handle; 56 const mpeg2_info_t *info; 57 void *buf; 58 ssize_t avail; 59 mpeg2_state_t state; 60 char format[256]; 61 char lformat[256]; 62 char gop_format[256]; 63 int have_gop; 64 uint64_t fsize; 65 unsigned int fail_count; 66 int did_seek; 67 int fmt1; 68 int fmt2; 69 int mime; 70 int fpal; 71 int fntsc; 72 int fsecam; 73 int fmac; 74 75 if (NULL == (handle = mpeg2_init ())) 76 return; 77 if (NULL == (info = mpeg2_info (handle))) 78 { 79 mpeg2_close (handle); 80 return; 81 } 82 fsize = ec->get_size (ec->cls); 83 buf = NULL; 84 have_gop = 0; 85 fail_count = 0; 86 did_seek = 0; 87 fmt1 = 0; 88 fmt2 = 0; 89 mime = 0; 90 fpal = 0; 91 fntsc = 0; 92 fsecam = 0; 93 fmac = 0; 94 lformat[0] = '\0'; 95 while (1) 96 { 97 state = mpeg2_parse (handle); 98 switch (state) 99 { 100 case STATE_BUFFER: 101 if (fail_count > 16) 102 goto EXIT; /* do not read large non-mpeg files */ 103 fail_count++; 104 if (0 >= (avail = ec->read (ec->cls, 105 &buf, 106 16 * 1024))) 107 goto EXIT; 108 mpeg2_buffer (handle, buf, buf + avail); 109 break; 110 case STATE_SEQUENCE: 111 fail_count = 0; 112 if (0 == mime) 113 { 114 mime = 1; 115 ADD ("video/mpeg", 116 EXTRACTOR_METATYPE_MIMETYPE); 117 } 118 snprintf (format, 119 sizeof(format), "%ux%u", 120 info->sequence->width, info->sequence->height); 121 if (0 != strcmp (lformat, 122 format)) 123 { 124 strcpy (lformat, 125 format); 126 ADD (format, EXTRACTOR_METATYPE_IMAGE_DIMENSIONS); 127 } 128 switch (info->sequence->flags & SEQ_VIDEO_FORMAT_UNSPECIFIED) 129 { 130 case SEQ_VIDEO_FORMAT_PAL: 131 if (0 == fpal) 132 { 133 fpal = 1; 134 ADD ("PAL", EXTRACTOR_METATYPE_BROADCAST_TELEVISION_SYSTEM); 135 } 136 break; 137 case SEQ_VIDEO_FORMAT_NTSC: 138 if (0 == fntsc) 139 { 140 fntsc = 1; 141 ADD ("NTSC", EXTRACTOR_METATYPE_BROADCAST_TELEVISION_SYSTEM); 142 } 143 break; 144 case SEQ_VIDEO_FORMAT_SECAM: 145 if (0 == fsecam) 146 { 147 fsecam = 1; 148 ADD ("SECAM", EXTRACTOR_METATYPE_BROADCAST_TELEVISION_SYSTEM); 149 } 150 break; 151 case SEQ_VIDEO_FORMAT_MAC: 152 if (0 == fmac) 153 { 154 fmac = 1; 155 ADD ("MAC", EXTRACTOR_METATYPE_BROADCAST_TELEVISION_SYSTEM); 156 } 157 break; 158 default: 159 break; 160 } 161 if ((info->sequence->flags & SEQ_FLAG_MPEG2) > 0) 162 { 163 if (0 == fmt1) 164 { 165 fmt1 = 1; 166 ADD ("MPEG2", EXTRACTOR_METATYPE_FORMAT_VERSION); 167 } 168 } 169 else 170 { 171 if (0 == fmt2) 172 { 173 fmt2 = 1; 174 ADD ("MPEG1", EXTRACTOR_METATYPE_FORMAT_VERSION); 175 } 176 } 177 if ( (0 == did_seek) && 178 (fsize != -1) && 179 (fsize > 1024 * 256 * 2) ) 180 { 181 /* skip to the end of the mpeg for speed */ 182 did_seek = 1; 183 ec->seek (ec->cls, 184 fsize - 256 * 1024, 185 SEEK_SET); 186 } 187 break; 188 case STATE_GOP: 189 fail_count = 0; 190 if ( (NULL != info->gop) && 191 (0 != info->gop->pictures) ) 192 { 193 snprintf (gop_format, 194 sizeof (gop_format), 195 "%02u:%02u:%02u (%u frames)", 196 info->gop->hours, 197 info->gop->minutes, 198 info->gop->seconds, 199 info->gop->pictures); 200 have_gop = 1; 201 } 202 break; 203 case STATE_SLICE: 204 fail_count = 0; 205 break; 206 case STATE_END: 207 fail_count = 0; 208 break; 209 case STATE_INVALID: 210 goto EXIT; 211 default: 212 break; 213 } 214 } 215 EXIT: 216 if (1 == have_gop) 217 ADD (gop_format, EXTRACTOR_METATYPE_DURATION); 218 mpeg2_close (handle); 219 } 220 221 222 /* end of mpeg_extractor.c */